package com.cdu.practicaltraining.sys.handler;

import com.cdu.practicaltraining.pojo.Dict;
import com.cdu.practicaltraining.pojo.DictDetail;
import com.cdu.practicaltraining.sys.service.DictService;
import com.cdu.practicaltraining.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/dict") // 字典模块
public class DictHandler {

    @Resource
    private DictService dictService;

    @PostMapping("/add")
    public CommonResult<Dict> addDict(@RequestBody Dict dict) {
        try {
            int i = dictService.insertDict(dict);
            if (i > 0) {
                return CommonResult.success(dict);
            }
            if (i == 0) {
                return CommonResult.error(500, "添加字典失败");
            }
            return CommonResult.error(-1, "您要添加的字典信息已经存在,请换一个.");
        } catch (Exception e) {
            e.printStackTrace();
            // dictService.insertDict(dict);出现异常怎么办？
            return CommonResult.error(503, "系统异常,添加失败,请联系管理员.");
        }
    }

    /**
     * 查询字典
     *
     * @param pageNum 页码
     * @param pageSize 每页显示的条数
     * @return
     */
    @GetMapping("/dicts/{pageNum}/{pageSize}")
    public CommonResult<PageInfo<Dict>> dicts(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
//            return dictService.dicts(pageNum,pageSize).size()>0 ?
//                    CommonResult.success(dictService.dicts(pageNum, pageSize)) :
//                    CommonResult.error(401,"查询字典失败,");

            List<Dict> dicts = dictService.dicts();
            PageInfo<Dict> pageInfo = new PageInfo<>(dicts);
            pageInfo.setPageNum(pageNum);
            pageInfo.setPageSize(pageSize);
            return dicts.size() > 0? CommonResult.success(pageInfo):CommonResult.error(401,"查询失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，查询失败，请联系管理员");
        }
    }

    /**
     * 根据id查询最新字典数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<Dict> findNewDict(@PathVariable("id") int id) {
        try {
            Dict dict = dictService.findById(id);
            return dict!=null? CommonResult.success(dict):CommonResult.error(401,"查询失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，查询失败，请联系管理员");
        }
    }

    @PutMapping("/update")
    public CommonResult updateDict(@RequestBody Dict dict) {
        try {
            int i = dictService.updateDict(dict);
            return i!=0?CommonResult.success(dict):CommonResult.error(500,"修改字典失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，修改失败，请联系管理员");
        }
    }

    /**
     * 逻辑删除字典
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public CommonResult deleteDict(@Param("id") long id) {
        try {
            int i = dictService.deleteDict(id);
            return i!=0?CommonResult.success(i):CommonResult.error(500,"删除字典失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，删除失败，请联系管理员");
        }
    }

    /**
     * 添加字典详情
     * @param dictDetail
     * @return
     */
    @PostMapping("/detail/add")
    public CommonResult addDictDetail(@RequestBody DictDetail dictDetail) {
        try {
            int i = dictService.insertDictDetail(dictDetail);
            if (i > 0) {
                return CommonResult.success(dictDetail);
            }
            if (i == 0) {
                return CommonResult.error(500, "添加字典明细失败");
            }
            return CommonResult.error(-1, "您要添加的字典明细信息已经存在,请换一个.");
        } catch (Exception e) {
            e.printStackTrace();
            // dictService.insertDict(dict);出现异常怎么办？
            return CommonResult.error(503, "系统异常,添加失败,请联系管理员.");
        }
    }

    /**
     * 查询字典列表
     * @param dtype 字典类型
     * @return CommonResult 统一返回类
     */
    @GetMapping("/detail/{dtype}/{pn}/{size}")
    public CommonResult<PageInfo<DictDetail>> findDictDetail(
            @PathVariable("dtype") String dtype,
            @PathVariable("pn") int pn,
            @PathVariable("size") int size
    ) {
        try {
            PageHelper.startPage(pn, size);
            List<DictDetail> dictDetails = dictService.findDictDetailByType(dtype);
            PageInfo<DictDetail> pageInfo = new PageInfo<>(dictDetails);
            return null != dictDetails ? CommonResult.success(pageInfo) : CommonResult.error(201, "请求成功,没有查询到字典详情数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(502, "系统异常,查询失败,请联系管理员.");
        }
    }

    /**
     * 查询字典详情
     * @param dtype 字典类型
     * @return CommonResult 统一返回类
     */
    @GetMapping("/detail/{dtype}")
    public CommonResult<List<DictDetail>> findDictDetail(@PathVariable("dtype") String dtype) {
        try {
            List<DictDetail> dictDetails = dictService.findDictDetailByType(dtype);
            return null != dictDetails ? CommonResult.success(dictDetails) : CommonResult.error(201, "请求成功,没有查询到字典详情数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(502, "系统异常,查询失败,请联系管理员.");
        }
    }
}